package com.baizhi.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
public class GetTestController {
    @Autowired
    private RestTemplate restTemplate;


    @RequestMapping("getTest")
    @HystrixCommand(fallbackMethod="getTestFallbackMethod")
    public String getTest(){
        /**
         * ResponseEntity 响应体 封装的有响应的状态码 和 响应信息 以及响应体
         */
      //  ResponseEntity<String> entity = restTemplate.getForEntity("http://client-1/test?name=zhangsan", String.class);
        String object = restTemplate.getForObject("http://client-1/test?name=zhangsan", String.class);
        System.out.println(object);
      //  String body = entity.getBody();
     //   System.out.println("body"+body);
       // HttpStatus statusCode = entity.getStatusCode();
        //System.out.println("statusCode"+statusCode);
        return object;
    }

    @RequestMapping("getTest1")
    @HystrixCommand(fallbackMethod = "getTestFallbackMethod")
    public String getTest1(){
        Map map = new HashMap();
        map.put("name","hhhh");
        map.put("password","123456");
        /**
         * ResponseEntity 响应体 封装的有响应的状态码 和 响应信息 以及响应体
         */
        String object = restTemplate.getForObject("http://client-1/testMap?name={name}&&password={password}", String.class,map);
        return object;
    }

    /**
     * 返回值类型和形惨必须一致
     * @return
     */
    public String getTestFallbackMethod(){
        return "请求失败 请稍后再试";
    }
}
